import {createRouter, createWebHistory} from 'vue-router'
import Login from "@/views/login/login.vue";
import Dashboard from '../views/admin/Dashboard.vue'
import StudentDashboard from "@/components/StudentDashboard.vue"
import AdminDashBoard from "@/components/AdminDashBoard.vue";
import CoachDashboard from "@/components/CoachDashboard.vue";

const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login,
        meta: {
            requireAuth: false,
            title: '用户登录 - 驾校管理系统'
        }
    },
    // {
    //     path: '/home',
    //     name: 'home',
    //     // route level code-splitting
    //     // this generates a separate chunk (about.[hash].js) for this route
    //     // which is lazy-loaded when the route is visited.
    //     component: function () {
    //         return import(/* webpackChunkName: "about" */ '../views/admin/HomePage.vue')
    //     }
    // },
    {
        path: '/',
        redirect: '/login',

    },
    {
        path: '/admin',
        component: AdminDashBoard,
        meta: {
            requireAuth: true,
            roles: ['admin'],//仅管理员可访问

        },
        children: [

            {
                path: '',
                redirect: {name: 'HomePage'}
            },
            {
                path: 'homepage',
                name: 'HomePage',
                component:()=>import('@/views/admin/HomePage.vue'),
                meta: { title: '管理首页' }
            },
            {
                path: 'category',
                name: 'Category',
                component: () => import('@/views/admin/CategoryView.vue'),
                meta: { title: '课程类别' }
            },
            {
                path: 'course',
                name: 'CourseView',
                component: () => import('@/views/admin/CourseView.vue'),
                meta: { title: '课程列表' }
            },
            {
                path: 'announcements',
                name: 'Announcements',
                component: () => import('@/views/admin/Announcements.vue'),
                meta: { title: '公告管理' }
            },
            {
                path: 'studentlist',
                name: 'StudentList',
                component: ()=>import('@/views/admin/StudentList.vue'),
                meta: { title: '学员管理' }
            },
            {
                path: 'coachlist',
                name: 'CoachList',
                component: ()=>import('@/views/admin/CoachList.vue'),
                meta: { title: '教练管理' }
            },
            {
                path: 'training_feedback',
                name: 'Admin_Training_Feedback',
                component: ()=>import('@/views/admin/Training_Feedback.vue'),
                meta: {title: '训练记录及反馈'}
            },
            {
                path: 'schedule',
                name: 'Schedule',
                component: ()=>import('@/views/admin/CoachSchedule.vue'),
                meta: { title: '课程安排' }

            },
            {
                path: 'appointment',
                name: 'Appointment',
                component: ()=>import('@/views/admin/Appointment.vue'),
                meta: { title: '预约管理'}
            },
        ]
    },
    // 教练段
    {
      path:'/coach',
      component: CoachDashboard,
      meta: {
          requireAuth: true,
          roles: ['coach']//仅教练可访问
      },
      children: [
          {
              path: 'coachHome',
              name: 'CoachHome',
              component: ()=>import('@/views/Coach/CoachHome.vue'),
              meta: {title: '教练首页'}
          },
          {
              path:'drivingInfo',
              name: 'DrivingInfo',
              component: ()=>import('@/views/Coach/DrivingSchoolInfo.vue'),
              meta: {title: '驾校信息'}
          },
          {
              path: 'coachProfile',
              name: 'CoachProfile',
              component: ()=>import('@/views/Coach/CoachProfile.vue'),
              meta: {title: '教练信息'}
          },
          {
              path: 'coachSchedule',
              name: 'CoachSchedule',
              component: ()=>import('@/views/Coach/CoachSchedule.vue'),
              meta: {title: '排班安排'}
          },
          {
              path: 'coachAnnouncement',
              name: 'CoachAnnouncement',
              component: ()=>import('@/views/Coach/CoachAnnouncement.vue'),
              meta: {title: '公告列表'}
          },

          {
              path: 'coachAppointment',
              name: 'CoachAppointment',
              component: ()=>import('@/views/Coach/CoachAppointment.vue'),
              meta: {title: '学生预约'}
          },
          {
              path: 'coachFeedback',
              name: 'CoachFeedback',
              component: ()=>import('@/views/Coach/CoachFeedback.vue'),
              meta: {title: '训练记录及反馈'}
          },
          {
              path: 'coachStudent',
              name: 'CoachStudent',
              component: ()=>import('@/views/Coach/CoachStudent'),
              meta: {title: '我的学生'}
          },

           {
          path: '',
          redirect: {name: 'CoachHome'}
            }


      ]
    },
//     学生端
    {
        path: '/student',
        component: StudentDashboard,
        meta: {
            requireAuth: true,
            roles: ['student']//仅学生可访问
        },
        children: [
            {
                path: 'studentHome',
                name: 'StudentHome',
                component: () => import('@/views/user/StudentHome.vue'),
                meta: {title: '学员首页'}
            },
            {
                path: 'drivingSchoolInfo',
                name: 'DrivingSchoolInfo',
                component: () => import('@/views/user/DrivingSchoolInfo.vue'),
                meta: {title: '驾校信息'}
            },
            {
                path: 'coachesList',
                name: 'CoachesList',
                component: () => import('@/views/user/CoachesList.vue'),
                meta: {title: '预约教练'}
            },
            {
                path: 'trainingSchedule',
                name: 'TrainingSchedule',
                component: () => import('@/views/user/TrainingSchedule.vue'),
                meta: {title: '考试训练安排'}
            },
            {
                path: 'announcementsList',
                name: 'AnnouncementsList',
                component: () => import('@/views/user/AnnouncementsList.vue'),
                meta: {title: '公告列表'}
            },
            {
                path: 'appointmentsList',
                name: 'AppointmentsList',
                component: () => import('@/views/user/AppointmentsList.vue'),
                meta: {title: '我的预约'}
            },
            {
                path: 'feedback',
                name: 'StudentFeedback',
                component: () => import('@/views/user/Feedback.vue'),
                meta: {title: '在线反馈'}
            },
            {
                path: 'studentProfile',
                name: 'StudentProfile',
                component: () => import('@/views/user/StudentProfile.vue'),
                meta: {title: '个人中心'}
            },
            {
                path: '',
                redirect: {name: 'StudentHome'}
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})
function getHomePathByRole(role) {
    switch (role) {
        case 'admin':
            return '/admin';
        case 'student':
            return '/student';
        case 'coach':
            return '/coach'; // 假设您有教练路由
        default:
            return '/login';
    }
}
// 添加路由守卫 - 基于角色控制访问权限
router.beforeEach((to, from, next)=>{
     // 模拟获取用户角色 (实际项目中应该从 store 或 API 获取)
    const role = (localStorage.getItem('role') || '').toLowerCase();
    const token = localStorage.getItem('access_token')
    // 白名单路由 - 无需权限即可访问
    const whitelist = ['/login']
    // 如果访问的是白名单路由，直接放行
    if(whitelist.includes(to.path)){
        if( token&& role){
            next(getHomePathByRole(role))
            return;
        }
        next()
        return
    }
    // 不需要权限的页面
    if(to.meta.requireAuth === false){
        next()
        return;
    }
    // 如果没有token，重定向到登录页
    if (!token || token === '') {
        next('/login');
        return;
    }
     // 如果没有角色信息，重定向到登录页
    if(!role ||role ===''){
        next('/login')
        return;
    }
    // 检查管理员权限
    if (to.path.startsWith('/admin')&&role !== 'admin'){
        next(getHomePathByRole(role))
        return
    }
    // 检查学生权限
    if (to.path.startsWith('/student')&&role !== 'student'){
        next(getHomePathByRole(role))
        return
    }
    // 检查教练权限
    if (to.path.startsWith('/coach')&&role !== 'coach'){
        next(getHomePathByRole(role))
        return
    }
    next()
})
router.afterEach((to, from) => {
    if(to.meta.title){
    document.title = `${to.meta.title} - 驾校管理系统`
}else{
        document.title = '驾校管理系统'
    }


})
export default router
